package com.asj.house.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.asj.house.entity.HouseIntermediary;
import com.asj.house.entity.HouseRoomIntermediary;
import com.asj.house.mapper.HouseIntermediaryMapper;
import com.asj.house.returns.RespCode;
import com.asj.house.returns.RespEntity;
import com.asj.house.returns.RespTable;
import com.asj.house.service.HouseIntermediaryService;
import com.asj.house.vo.param.RoomParam;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 中介信息表 服务实现类
 * </p>
 *
 * @author asj
 * @since 2021-03-16
 */
@Service
public class HouseIntermediaryServiceImpl extends ServiceImpl<HouseIntermediaryMapper, HouseIntermediary> implements HouseIntermediaryService {

    @Override
    public RespTable<List<HouseIntermediary>> listHouseRoomIntermediary(RoomParam roomParam) {
        QueryWrapper<HouseIntermediary> queryWrapper = new QueryWrapper<>();
        if (Objects.nonNull(roomParam) && StringUtils.isNotBlank(roomParam.getName())) {
            queryWrapper.lambda().like(HouseIntermediary::getName, roomParam.getName());
        }
        queryWrapper.lambda().eq(HouseIntermediary::getDeleted, 0)
                .orderByDesc(HouseIntermediary::getId);

        IPage<HouseIntermediary> iPage = new Page<>(roomParam.getPage(), roomParam.getLimit());
        IPage<HouseIntermediary> page = super.page(iPage, queryWrapper);

        if (CollectionUtil.isEmpty(page.getRecords())) {
            return new RespTable<>(RespCode.OK, new ArrayList<>(), page.getTotal());
        }

        return new RespTable<>(RespCode.OK, page.getRecords(), page.getTotal());
    }

    @Override
    public RespEntity insertHouseIntermediary(HouseIntermediary houseIntermediary) {
        baseMapper.insert(houseIntermediary);
        return new RespEntity(RespCode.SUCCESS);
    }

    @Override
    public RespEntity updateHouseIntermediary(HouseIntermediary houseIntermediary) {
        baseMapper.updateById(houseIntermediary);
        return new RespEntity(RespCode.SUCCESS);
    }

    @Override
    public RespEntity deleteHouseIntermediary(Integer id) {
        HouseIntermediary houseIntermediary = new HouseIntermediary();
        houseIntermediary.setId(id);
        houseIntermediary.setDeleted(1);
        baseMapper.updateById(houseIntermediary);
        return new RespEntity(RespCode.SUCCESS);
    }

    @Override
    public RespEntity deleteBatchHouseIntermediary(Integer[] ids) {
        for (Integer id : ids) {
            HouseIntermediary houseIntermediary = new HouseIntermediary();
            houseIntermediary.setId(id);
            houseIntermediary.setDeleted(1);
            baseMapper.updateById(houseIntermediary);
        }
        return new RespEntity(RespCode.SUCCESS);
    }
}
